﻿#region GNU GPL Version 3 License

/// Copyright 2013 Shailesh Lolam
/// 
/// This file Api.cs is part of AzEzAuto.
/// 
/// AzEzAuto is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
/// 
/// AzEzAuto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
/// 
/// You should have received a copy of the GNU General Public License along with AzEzAuto. If not, see http://www.gnu.org/licenses/.
/// 
/// History:
/// ______________________________________________________________
/// Created         04-2013             Shailesh Lolam

#endregion
      
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using AzEzAuto.Utils;
using AzEzAuto.Entities;
using System.Collections.Specialized;

namespace AzEzAuto.Management
{
    public class Api
    {
        protected const string VERSION = "2012-03-01";
        protected const string VERSION_TAG = "x-ms-version";
        protected const string REQUEST_ID = "x-ms-request-id";


        private HttpApi _httpApi;
        private string _version;
        private Cloud _cloud;
        private Storage _storage;
        public Api(X509Certificate2 certificate)
            : this(certificate, VERSION)
        {
        }

        public Api(X509Certificate2 certificate, string version)
        {
            _httpApi = new HttpApi(certificate);
            _cloud = new Cloud(_httpApi, version);
            _storage = new Storage(_httpApi, version);
            _version = version;
        }

        public OperationStatus GetRequestStatus(string subscriptionId, string requestId)
        {
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            return _httpApi.Get<OperationStatus>(new Uri(string.Format("https://management.core.windows.net/{0}/operations/{1}", subscriptionId, requestId)), headers);
        }

        public Locations GetLocations(string subscriptionId)
        {
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            return _httpApi.Get<Locations>(new Uri(string.Format("https://management.core.windows.net/{0}/locations", subscriptionId)), headers);
        }

        public AffinityGroups GetAffinityGroups(string subscriptionId)
        {
            Guard.CheckSubscriptionId(subscriptionId);
            NameValueCollection headers = new NameValueCollection();
            headers[VERSION_TAG] = _version;
            return _httpApi.Get<AffinityGroups>(new Uri(string.Format("https://management.core.windows.net/{0}/affinitygroups", subscriptionId)), headers);
        }

        public Cloud Cloud
        { get { return _cloud; } }

        public Storage Storage
        { get { return _storage; } }
    }
}
